﻿using System;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using System.Text;
using Texfina.Core.Common;
using Texfina.Common.Mg;
using Texfina.Core.Web;
using Texfina.Core.Data;
using Texfina.Entity.Pd;
using Texfina.Entity.Mg;
using Texfina.Entity.Sy;
using PDBOQry = Texfina.BOQry.Pd;
using MGBOQry = Texfina.BOQry.Mg;
//using PDBOMnt = Texfina.BOMnt.Pd;
using SYBOQry = Texfina.BOQry.Sy;

namespace PD
{
    public partial class wfMaquinaMnt : EditPage
   {
      protected void Page_Load(object sender, EventArgs e)
      {
         if (this.IsPostBack)
         {
            this.LoadInPostBack();
            return;
         }
         this.LoadData();
      }

      public override void LoadInPostBack()
      {
         this.RegisterGeneralScripts();
      }

      public override void LoadInitialSettings()
      {
         this.FormEditAction = (FormEditAction)Enum.Parse(typeof(FormEditAction), Request.QueryString["formEA"]);
         this.SetInitialTitle("Maquinas");
      }

      public override void LoadListControls()
      {
          ETablaGenDet objEDet = new ETablaGenDet();
          MGBOQry.ITablaGen objBOI = (MGBOQry.ITablaGen)WCFHelper.GetObject<MGBOQry.ITablaGen>(typeof(MGBOQry.TablaGen));

          objEDet.IdTabla = ConstMultitabla.TIPO_MAQUINA;
          DataTable dt = objBOI.GetDetailByList(objEDet);
          WebHelper.LoadListControl(ddlidTipMaquina, dt, "dsDescrip", "idCodigo", true, true);


      }

      public override void DataToControls()
      {
          EMaquinas objE = new EMaquinas();
          IBOQuery objBO = (IBOQuery)WCFHelper.GetObject<IBOQuery>(typeof(PDBOQry.Maquinas));
          objE.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
          objE.IdMaquina = Request.QueryString["idMaquina"];
          objE = (EMaquinas)objBO.GetByKey(objE);


          if (objE != null)
          {

                WebHelper.EntityToControl(objE, "IdMaquina", txtidMaquina);
                WebHelper.EntityToControl(objE, "DsMaquina", txtdsMaquina);

                WebHelper.EntityToControl(objE, "MtFacAbs", txtFacAbs);
                WebHelper.EntityToControl(objE, "IdMaquinaAux", txtidMaquinaAux);
                WebHelper.EntityToControl(objE, "MtCapacidad", txtnuCapacidad);
                WebHelper.EntityToControl(objE, "MtCostoMin", txtnuCosto);
                WebHelper.EntityToControl(objE, "MtHorasAsis", txtnuHoras);
                WebHelper.EntityToControl(objE, "NuMicro", txtnuMicro);
                WebHelper.EntityToControl(objE, "IdRelBan", txtnuRb);
                WebHelper.EntityToControl(objE, "NuTempMax", txtnuTempMax);
                WebHelper.EntityToControl(objE, "NuTubosMaq", txtnuTubos);
                WebHelper.EntityToControl(objE, "NuVolMax", txtnuVolumenMax);
                WebHelper.EntityToControl(objE, "NuVolMed", txtnuVolumenMed);
                WebHelper.EntityToControl(objE, "NuVolMin", txtnuVolumenMin);
                WebHelper.EntityToControl(objE, "NuVolResiduo", txtnuVolumenRes);
                WebHelper.EntityToControl(objE, "NuVolTope", txtnuVolumenTope);
                WebHelper.EntityToControl(objE, "MtPesoMax", txtPesoMaximo);
                WebHelper.EntityToControl(objE, "MtPesoMin", txtPesoMinimo);
                WebHelper.EntityToControl(objE, "MtPesoMed", txtPesoMedio);

                WebHelper.EntityToControl(objE, "IdTipoMaquina", ddlidTipMaquina);

              //WebHelper.EntityToControl(objE, "NuProcAnual", txtnuProcAnual);

          }


          //EPlanillaDet objEPlaDet = new EPlanillaDet();

          //RHBOQry.Planilla objBOPerC = (RHBOQry.Planilla)WCFHelper.GetObject<RHBOQry.Planilla>(typeof(RHBOQry.Planilla));
          //objEPlaDet.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
          //objEPlaDet.IdPlanilla = Request.QueryString["idPlanilla"];
          //DataTable dt = objBOPerC.GetByParentKeyPlaniDet(objEPlaDet);

          //this.GridSetDataBinding(grdList, dt);
          //this.GridSort(grdList, "nuSecuen", SortDirection.Ascending);


      }

      public override void RegisterGeneralScripts()
      {
         this.txtidMaquina.Attributes.Add("readonly", "true");
         this.txtdsMaquina.Attributes.Add("readonly", "true");
         //this.txtidMes.Attributes.Add("readonly", "true");
         //this.txtfeDesde.Attributes.Add("readonly", "true");
         //this.TxtfeHasta.Attributes.Add("readonly", "true");

         StringBuilder sb = new StringBuilder();

         sb.AppendLine("function validateEditForm(key){");

         sb.AppendLine("var varRpta = isEmptyControl('" + this.txtidMaquina.ClientID + "','Seleccione Planilla');");
         sb.AppendLine("if(varRpta==true)");
         sb.AppendLine("return !varRpta;");
         //sb.AppendLine("var varRpta = isEmptyControl('" + this.ddlidMoneda.ClientID + "','Seleccione Moneda');");
         //sb.AppendLine("if(varRpta==true)");
         //sb.AppendLine("return !varRpta;");
         //sb.AppendLine("var varRpta = isEmptyControl('" + this.txtMtc.ClientID + "','Ingrese Monto Tipo Cambio(Mtc)');");
         //sb.AppendLine("if(varRpta==true)");
         sb.AppendLine("return !varRpta;");
         sb.AppendLine("menu_doPostBack('" + mnuToolbar.ClientID + "','" + mnuToolbar.UniqueID + "',key);");
         sb.AppendLine("}");

         ScriptManager.RegisterStartupScript(this, this.GetType(), "ScriptRegister", sb.ToString(), true);

         StringBuilder sb2 = new StringBuilder();


         //sb2.AppendLine("var varRet = modalDialog('wfPeriodoPlani.aspx',750,400);");
         //sb2.AppendLine("if(varRet==null)");
         //sb2.AppendLine("return false;");
         //sb2.AppendLine("ctl = document.getElementById('" + this.hdnidForPago.ClientID + "');");
         //sb2.AppendLine("ctl.value = varRet[0];");
         //sb2.AppendLine("ctl = document.getElementById('" + this.txtidForPago.ClientID + "');");
         //sb2.AppendLine("ctl.value = varRet[1];");
         //sb2.AppendLine("ctl = document.getElementById('" + this.txtidMaquina.ClientID + "');");
         //sb2.AppendLine("ctl.value = varRet[2];");
         //sb2.AppendLine("ctl = document.getElementById('" + this.txtdsMaquina.ClientID + "');");
         //sb2.AppendLine("ctl.value = varRet[3];");
         //sb2.AppendLine("ctl = document.getElementById('" + this.txtidPlanilla.ClientID + "');");
         //sb2.AppendLine("ctl.value = varRet[3];");
         //sb2.AppendLine("ctl = document.getElementById('" + this.txtNroProcAnual.ClientID + "');");
         //sb2.AppendLine("ctl.value = varRet[4];");
         //sb2.AppendLine("ctl = document.getElementById('" + this.hdnidMes.ClientID + "');");
         //sb2.AppendLine("ctl.value = varRet[5];");
         //sb2.AppendLine("ctl = document.getElementById('" + this.txtidMes.ClientID + "');");
         //sb2.AppendLine("ctl.value = varRet[6];");
         //sb2.AppendLine("ctl = document.getElementById('" + this.txtfeDesde.ClientID + "');");
         //sb2.AppendLine("ctl.value = varRet[7];");
         //sb2.AppendLine("ctl = document.getElementById('" + this.TxtfeHasta.ClientID + "');");
         //sb2.AppendLine("ctl.value = varRet[8];");

         //this.txtidPlanilla.OnClientClick = sb2.ToString();

         ScriptManager.RegisterStartupScript(this, this.GetType(), "ScriptRegister", sb2.ToString(), true);
      }

      public override void LockControls()
      {
         if (this.FormEditAction != FormEditAction.AddNew)
         {
            //txtidMaquina.EnabledButton = false;
         }
      }

      protected void mnuToolbar_MenuItemClick(Object sender, MenuEventArgs e)
      {
         if (e.Item == null)
            return;

         switch (e.Item.Value)
         {
            case ConstMenuToolbarValue.BUTTON_SAVE:
            case ConstMenuToolbarValue.BUTTON_SAVENEW:
               if (this.FormEditAction != FormEditAction.Delete)
                  this.SaveData(e.Item.Value);
               else
                  this.DeleteData();
               break;

            case ConstMenuToolbarValue.BUTTON_EXIT:
               this.Response.Redirect("wfMaquinaBnd.aspx");
               break;
         }

      }


      public override Object CreateEntitySet()
      {
         EMaquinas objE = new EMaquinas();

         if (this.FormEditAction == FormEditAction.AddNew || this.FormEditAction == FormEditAction.Copy)
            objE.EntityState = EntityState.Added;
         else
         {

            IBOQuery objBO = (IBOQuery)WCFHelper.GetObject<IBOQuery>(typeof(PDBOQry.Maquinas));
            objE.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
            objE.IdMaquina = Request.QueryString["IdMaquina"];


            if (this.FormEditAction == FormEditAction.Edit)
               objE.EntityState = EntityState.Modified;
            if (this.FormEditAction == FormEditAction.Delete)
               objE.EntityState = EntityState.Deleted;

         }

         objE.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();

         //WebHelper.ControlToEntity(hdnidMes, objE, "IdMes");
         //WebHelper.ControlToEntity(hdnidForPago, objE, "IdForPago");
         WebHelper.ControlToEntity(txtidMaquina, objE, "IdMaquina");
         WebHelper.ControlToEntity(txtdsMaquina, objE, "DsMaquina");

         WebHelper.ControlToEntity(txtFacAbs, objE, "MtFacAbs");
         WebHelper.ControlToEntity(txtidMaquinaAux, objE, "IdMaquinaAux");
         WebHelper.ControlToEntity(txtnuCapacidad, objE, "MtCapacidad");
         WebHelper.ControlToEntity(txtnuCosto, objE, "MtCostoMin");
         WebHelper.ControlToEntity(txtnuHoras, objE, "MtHorasAsis");
         WebHelper.ControlToEntity(txtnuMicro, objE, "NuMicro");
         WebHelper.ControlToEntity(txtnuRb, objE, "IdRelBan");
         WebHelper.ControlToEntity(txtnuTempMax, objE, "NuTempMax");
         WebHelper.ControlToEntity(txtnuTubos, objE, "NuTubosMaq");
         WebHelper.ControlToEntity(txtnuVolumenMax, objE, "NuVolMax");
         WebHelper.ControlToEntity(txtnuVolumenMed, objE, "NuVolMed");
         WebHelper.ControlToEntity(txtnuVolumenMin, objE, "NuVolMin");
         WebHelper.ControlToEntity(txtnuVolumenRes, objE, "NuVolResiduo");
         WebHelper.ControlToEntity(txtnuVolumenTope, objE, "NuVolTope");

         WebHelper.ControlToEntity(txtPesoMaximo, objE, "MtPesoMax");
         WebHelper.ControlToEntity(txtPesoMinimo, objE, "MtPesoMin");
         WebHelper.ControlToEntity(txtPesoMedio, objE, "MtPesoMed");


         
         //WebHelper.ControlToEntity(ddlidMoneda, objE, "IdMoneda");
         //WebHelper.ControlToEntity(txtMtc, objE, "MtTC");
         //WebHelper.ControlToEntity(txtfeDesde, objE, "FeDesde");
         //WebHelper.ControlToEntity(TxtfeHasta, objE, "FeHasta");
         //objE.FeProceso = DateTime.Now;
         //objE.StProceso = ConstEstadoProcePlani.ABIERTA;

         if (this.FormEditAction == FormEditAction.Delete)
         {
            objE.StAnulado = ConstFlagEstado.ACTIVADO;
         }
         else
         {
            objE.StAnulado = ConstFlagEstado.DESACTIVADO;
         }

         return objE;
      }



   }
}